<template>
  <div class="login-page">
    <div class="login-container">
      <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
        <h3 class="login-title"><i class="el-icon-user"></i> 欢迎登录</h3>
        <el-form-item label="账号" prop="username">
          <el-input type="text" placeholder="请输入账号" v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ required: true, message: "账号不可为空", trigger: "blur" }],
        password: [{ required: true, message: "密码不可为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios
            .get("http://localhost:8077/login", {
              params: {
                msgId: "257",
                username: this.form.username,
                password: this.form.password
              }
            })
            .then(res => {
              console.log(res);
              if (res.data.code != 200) {
                alert("用户名、密码错误");
                return;
              }
              let sessionId = res.data.data;
              this.$store.dispatch("asyncUpdateUser", { sessionId: sessionId });
              console.log("this.$store.state=", this.$store.state);
              this.$router.push("/main");
            });
        } else {
          alert("请输入用户名和密码");
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
/* 页面背景样式 */
.login-page {
  background-image: url('https://haowallpaper.com/link/common/file/getCroppingImg/15723786602451264');
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 登录框样式 */
.login-container {
  background: rgba(255, 255, 255, 0.9); /* 半透明背景 */
  border-radius: 10px;
  padding: 40px 30px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  width: 400px;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
  font-size: 24px;
  font-weight: bold;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-title i {
  margin-right: 8px;
  color: #409eff;
}

/* 输入框样式 */
.el-input__inner {
  border-radius: 5px;
}

/* 按钮样式 */
.login-button {
  width: 100%;
  border-radius: 5px;
  font-size: 16px;
  font-weight: bold;
  background-color: #409eff;
  border: none;
  transition: background-color 0.3s ease;
}

.login-button:hover {
  background-color: #66b1ff;
}

/* 提示文本样式 */
.el-form-item__label {
  color: #606266;
  font-size: 14px;
}

/* 阴影样式 */
.login-box {
  border: none;
  box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.1);
}
</style>
